#coding=utf-8
import re
import utility as u

stock_url = 'http://money.finance.sina.com.cn/corp/go.php/vCI_CorpInfo/stockid/%s.phtml'

def parse_html(html):
    stock = {}
    
    r = re.compile(r'证券简称：(.+)<')
    name = r.search(html).group(1)    
    stock['name'] = name
    
    r = re.compile(r'交易所：(.+)<')
    market = r.search(html).group(1)
    stock['market'] = u.markets_for_short[market]
    
    r = re.compile(r'上市日期：.+>(.+)</a>')
    ipo = r.search(html).group(1)
    stock['ipo'] = ipo
    
    r = re.compile(r'发行价格：.+>(.+)</a>')
    iprice = r.search(html).group(1)
    stock['iprice'] = iprice
    
    return stock

def get_stock(code):
    url = stock_url%code
    html = u.urlopen(url).read().decode('gbk').encode('u8')
    stock = parse_html(html)
    stock['code'] = code
    return stock
